package com.yyaccp.appinfo.util;

import com.alibaba.fastjson.JSONObject;
import com.yyaccp.appinfo.domain.DevUser;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter;

public class SysInterceptor extends HandlerInterceptorAdapter {

    public boolean perHandle(HttpServletRequest request, HttpServletResponse response
            ,Object handler)throws Exception{
        HttpSession session = request.getSession();
        DevUser devUser = (DevUser)session.getAttribute(Constants.LOGIN_USER);
        if(devUser == null){
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out = response.getWriter();
            String json = JSONObject.toJSONString(AjaxResult.error("请先登录"));
            out.print(json);
            out.flush();
            return false;
        }
        return true;
    }
}
